// See LICENSE for license details.

#ifndef _MMAP_H
#define _MMAP_H

#include "syscall.h"
#include "types.h"
#include "functions.h"
#include "config.h"
#include "atomic.h"
#include "console.h"
#include <string.h>
#include <stddef.h>


#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4

#define MAP_PRIVATE 0x2
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define MAP_POPULATE 0x8000
#define MREMAP_FIXED 0x2

extern int demand_paging;
extern uintptr_t mem_size;



void map_kernel_range(uintptr_t vaddr, uintptr_t paddr, size_t len, int prot);
pte_t * walk_user_pagetable(pte_t * pagetable, uint64 addr, int create);
int mappages(pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm);
uint64 uvmalloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz);

void uvmprint(pte_t *  pagetable);
pte_t prot_to_type(int prot, int user);

uint64 va2pa(const void * va);
void uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free);
int uvmcopy(pagetable_t old, pagetable_t new, uint64 sz);
void proc_freepagetable(pagetable_t pagetable, uint64 sz,uint64 trapframe);
void freewalk(pagetable_t pagetable);
int handle_page_fault(uint64 fault_vaddr ,int plot);
int copyout(pagetable_t pagetable, uint64 dstva, char *src, uint64 len);


#endif
